package com.example.stub;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class SimulatorClient {

	private final static Logger log = LoggerFactory.getLogger(SimulatorClient.class);
	
	protected int threadCount;
	
	protected int threadInterval;
	
	protected abstract SimulatorThread createSimulatorThread(int no, int totalThread) throws Exception;
	
	public SimulatorClient(int threadCount, int threadInterval){
		this.threadCount = threadCount;
		this.threadInterval = threadInterval;
	}
	
	public SimulatorClient(int threadCount){
		this.threadCount = threadCount;
	}

	public void execute() throws Exception {
		List<Thread> threads = new ArrayList<>();
		
		for(int i = 0; i < threadCount; i++){
			threads.add(createSimulatorThread(i + 1, threadCount));
		}
		
		for(int i = 0; i < threadCount; i++) {
			final int no = i;
			threads.get(i).start();
			
			try {
				Thread.sleep(threadInterval);
			} catch (InterruptedException e) {
				log.error("failed to sleep thread [" + no + "]");
			}
		}
	}
	
	public int getThreadInterval() {
		return threadInterval;
	}

	public void setThreadInterval(int threadInterval) {
		this.threadInterval = threadInterval;
	}

}
